package com.estate.struts.actions;

import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;

import com.estate.business.PriceProjectService;
import com.estate.business.ProFileService;
import com.estate.business.ProSuggestionService;
import com.estate.business.RecordMemberService;
import com.estate.business.UserInfoService;
import com.estate.vo.Vo;
import com.estate.vo.impl.PriceProjectVo;
import com.estate.vo.impl.ProSuggestionVo;
import com.estate.vo.impl.RecordMemberVo;
import com.estate.vo.impl.UserInfoVo;
import com.estate.utils.ProCatagoryUtil;
import com.estate.utils.StageUtil;

public class PriceProjectAction extends EstateSupport implements
		ServletRequestAware, ServletResponseAware {

	private PriceProjectService priceProjectService;
	private UserInfoService userInfoService;
	private ProFileService proFileService;
	private RecordMemberService recordMemberService;
	private ProSuggestionService proSuggestionService;

	private String method;
	private String deleteId;
	private String checkId;
	private String modifyId;
	private String regeditId;
	private String checkPage;
	private String currentStage;
	private String multiDelete;
	private String delStr;
	private String multiType;
	private String outputValue;

	private int id;
	private String number; // 项目编号
	private String name; // 项目名称
	private String delegateCompany; // 委托单位
	private String delegateUser; // 委托单位联系人
	private String delegatePhone; // 委托单位联系人电话
	private String proCatagory; // 工作项目类别
	private Date receiveDate; // 收到日期
	private String contractId; // 合同ID
	private String estimatePrice; // 合同估算价
	private String submitPrice; // 送审价
	private String proRegion; // 所属地区
	private String industryCatagory; // 行业分类
	private int submitUserId; // 填写人
	private String remark; // 备注
	private String proName; // 单项工程名称
	private String proCompany; // 建设单位名称
	private String proContact; // 建设单位联系人
	private String proContactPhone; // 建设单位联系人电话
	private String workCatagory; // 工作项目分类
	private int stage; // 阶段
	private Date authorDate; // 下发日期
	private int submit; // 是否已上报
	private int recordUserId; // 咨询人员ID
	private UserInfoVo contactUser;
	private int contactUserId;
	private int leaderId;
	private String userStr;
	private List<String> memList;
	private int trailState;
	private int isMajor;

	private HttpServletRequest request;
	private HttpServletResponse response;

	public String execute() throws Exception {
		String page = "";
		System.out.println(this.getMethod());
		if ("list".equals(this.getMethod())) {
			System.out.println("run success");
			List<PriceProjectVo> priceProjectList = priceProjectService
					.findByStage(1);
			for (int i = 2; i <= 12; i++) {
				priceProjectList.addAll(priceProjectService.findByStage(i));
			}
			for (int i = 0; i < priceProjectList.size(); i++) {
				if (priceProjectList.get(i).getStage() > 1) {
					priceProjectList.get(i).setStageName(
							StageUtil.getStageString(2));
				} else {
					priceProjectList.get(i).setStageName(
							StageUtil.getStageString(1));
				}
			}
			if (this.getMultiType() != null
					&& this.getMultiType().equals("true")) {
				this.getRequest().setAttribute("multiType", new Boolean(true));
			} else {
				this.getRequest().setAttribute("multiType", new Boolean(false));
			}
			ServletActionContext.getRequest().setAttribute("priceProjectList",
					priceProjectList);

			// String id="user_table";
			// TableFacade tableFacade = new TableFacadeImpl("user_table",
			// ServletActionContext.getRequest());
			page = "list";

		} else if ("add".equals(method)) {
			priceProjectService.addPriceProject(this.getAddProjectInfo());
			List<Vo> voList=this.addMemberInfo();
			for(int i=0;i<voList.size();i++){
				((RecordMemberVo)voList.get(i)).setName(((RecordMemberVo)voList.get(i)).getRecordUser().getName());
			}
			System.out.println(voList.size()+"volist.size");
			if(voList.size()==0){
				page="delete";
			} else{
				this.getSession().setAttribute("memberList", voList);
			page = "add";}
		} else if ("load".equals(method)) {
			System.out.println("in load");
			HashMap<String, Integer> map = userInfoService.findAllNameAndId();
			// System.out.println(map.get("22"));
			ServletActionContext.getRequest().setAttribute("contactUserSelect",
					map);
			page = "load";
		} else if ("check".equals(method)) {
			this.getRequest().setAttribute("page", this.checkPage);
			PriceProjectVo checkList = (PriceProjectVo) priceProjectService
					.findByPK(Integer.parseInt(this.getCheckId()));
			if (checkList.getContactUser() != null) {
				this.getRequest().setAttribute("contactUserId",
						checkList.getContactUser().getName());
			} else
				this.getRequest().setAttribute("contactUserId", "还没认命组长");
			List<ProSuggestionVo> suggestionList=proSuggestionService.getVoByPro(1, Integer.parseInt(this.getCheckId()));
			for(int i=0;i<suggestionList.size();i++){
				suggestionList.get(i).setStatusName(StageUtil.getStageString(suggestionList.get(i).getProStatus()));
			}
			this.getRequest().setAttribute("suggestionList",suggestionList);
			List<Vo> memberList = recordMemberService.findMemberList(1, Integer
					.parseInt(this.getCheckId()));
			for (int i = 0; i < memberList.size(); i++) {
				((RecordMemberVo)memberList.get(i)).setName(((RecordMemberVo)memberList.get(i)).getRecordUser().getName());
			}
			this.getRequest().setAttribute("memberList", memberList);
			this.getRequest().setAttribute("fileList",
					proFileService.getFileList(1, checkList.getId()));
			changeLabel();
			this.getRequest().setAttribute("checkList", checkList);
			this.getRequest().setAttribute("trailPlan", proFileService.getTrailPlanFiles(Integer.parseInt(this.getCheckId())));
			this.getRequest().setAttribute("trailSum", proFileService.getTrailSumFiles(Integer.parseInt(this.getCheckId())));
			this.getRequest().setAttribute("majorPlan", proFileService.getMajorPlanFiles(Integer.parseInt(this.getCheckId())));
			this.getRequest().setAttribute("majorSum", proFileService.getMajorSumFiles(Integer.parseInt(this.getCheckId())));
			page = "check";
		} else if ("regedit".equals(method)) {
			System.out.println("in regedit");
			PriceProjectVo regeditVo = (PriceProjectVo) priceProjectService
					.findByPK(Integer.parseInt(regeditId));
			regeditVo.setStage(2);
			priceProjectService.updateVo(regeditVo);
			page = "delete";
		} else if ("modify".equals(method)) {
			PriceProjectVo modifyList = (PriceProjectVo) priceProjectService
					.findByPK(Integer.parseInt(this.getModifyId()));
			this.getRequest().setAttribute("modifyList", modifyList);
			this.getRequest().setAttribute("contactUser",
					modifyList.getContactUser());
			List<Vo> memberList =recordMemberService.findMemberList(1, Integer.parseInt(this.getModifyId()));
			for (int i = 0; i < memberList.size(); i++) {
				((RecordMemberVo)memberList.get(i)).setName(((RecordMemberVo)memberList.get(i)).getRecordUser().getName());
			}
			this.getRequest().setAttribute("memberList", memberList);
			HashMap<String, Integer> map = userInfoService.findAllNameAndId();
			// System.out.println(map.get("22"));
			ServletActionContext.getRequest().setAttribute("contactUserSelect",
					map);
			this.getRequest().setAttribute("trailPlan", proFileService.getTrailPlanFiles(Integer.parseInt(this.getModifyId())));
			this.getRequest().setAttribute("trailSum", proFileService.getTrailSumFiles(Integer.parseInt(this.getModifyId())));
			this.getRequest().setAttribute("majorPlan", proFileService.getMajorPlanFiles(Integer.parseInt(this.getModifyId())));
			this.getRequest().setAttribute("majorSum", proFileService.getMajorSumFiles(Integer.parseInt(this.getModifyId())));
			page = "modify";
		} else if ("outputValue".equals(method)) {
			System.out.println("in check" + checkId);
			PriceProjectVo checkList = (PriceProjectVo) priceProjectService
					.findByPK(Integer.parseInt(this.getCheckId()));
			System.out.println(checkList);
			
			this.getRequest().setAttribute("checkList", checkList);
			page = "outputValue";
			System.out.println(page);
		} else if ("value".equals(method)) {
			System.out.println("in value" + "!" + this.getId());
			PriceProjectVo vo = (PriceProjectVo) priceProjectService
					.findByPK(this.getId());
			vo.setOutputValue(this.getOutputValue());
			priceProjectService.updateVo(vo);
			page = "value";
		} else if ("valueList".equals(method)) {
			List<Vo> priceProjectList = priceProjectService
					.findAllPriceProject();
			ServletActionContext.getRequest().setAttribute("costList",
					priceProjectList);
			page = "valueList";
		} else if ("leader".equals(method)) {
			JSONArray memberArr = getMemberArr(this.getLeaderId());
			response.setContentType("text/html;charset=UTF-8");
			System.out.println("3333");
			PrintWriter out = this.response.getWriter();
			System.out.println("2222");
			out.print(memberArr);
			out.flush();
			out.close();
			page = null;
		} else if("member".equals(method)){
			System.out.println(userStr);
			String[] delArray = userStr.split(",");
			
			for (int i = 0; i < delArray.length; ){
				RecordMemberVo vo=recordMemberService.findByPK(Integer.parseInt(delArray[i]));
				vo.setMemberJob(new String(delArray[i+1].getBytes("ISO-8859-1"),"UTF-8"));
				recordMemberService.update(vo);
				i=i+2;
			}
			page="delete";
		} else if("trailState".equals(method)){
			List<PriceProjectVo> priceProjectList = priceProjectService.findByTrail(1);
			ServletActionContext.getRequest().setAttribute("trailList",
			priceProjectList);
			page = "trail";
		} else if("isMajor".equals(method)){
			List<PriceProjectVo> priceProjectList = priceProjectService.findByMajor(1);
			ServletActionContext.getRequest().setAttribute("majorList",
			priceProjectList);
			page = "major";
		}
		System.out.println("~~~~~~~~~~~~~~~~~~~" + page);
		return page;
	}

	public List<Vo> addMemberInfo() {
		List<Vo> memberList=new ArrayList<Vo>();
		if (userStr != null && !"".equals(userStr) && !",".equals(userStr)) {
			List<Vo> voList = priceProjectService.findAllPriceProject();
			int lastInt = ((PriceProjectVo) voList.get(voList.size() - 1))
					.getId();
			String[] delArray = userStr.split(",");
			
			for (int i = 0; i < delArray.length; i++) {
				RecordMemberVo vo = new RecordMemberVo();
				vo.setProCatagory(1);
				vo.setProId(lastInt);
				vo.setRecordUser((UserInfoVo) userInfoService
						.getUserInfo((Integer.parseInt(delArray[i]))));
				memberList.add(vo);
				recordMemberService.addMember(vo);
			}
				
		}
		return memberList;
	}

	public JSONArray getMemberArr(int leaderId) {
		System.out.println(leaderId);
		JSONArray memberArr = new JSONArray();
		List<Vo> voList = userInfoService.findAll();
		System.out.println(((UserInfoVo) userInfoService.getUserInfo(leaderId))
				.getId());
		System.out.println(voList.size());
		for (int i = 0; i < voList.size(); i++) {
			if (((UserInfoVo) voList.get(i)).getId() == leaderId)
				continue;
			JSONObject obj = new JSONObject();
			System.out.println(i);
			obj.put("id", ((UserInfoVo) voList.get(i)).getId());
			obj.put("name", ((UserInfoVo) voList.get(i)).getName());
			System.out.println(obj);
			memberArr.add(obj);
		}
		System.out.println(memberArr);
		return memberArr;
	}

	public PriceProjectVo getAddProjectInfo() {
		PriceProjectVo priceProjectVo = new PriceProjectVo();
		priceProjectVo.setAuthorDate(authorDate);
		priceProjectVo.setContractId(contractId);
		priceProjectVo.setDelegateCompany(delegateCompany);
		priceProjectVo.setDelegatePhone(delegatePhone);
		priceProjectVo.setDelegateUser(delegateUser);
		priceProjectVo.setEstimatePrice(estimatePrice);
		priceProjectVo.setIndustryCatagory(industryCatagory);
		priceProjectVo.setName(name);
		priceProjectVo.setIsMajor(isMajor);
		priceProjectVo.setTrailState(trailState);
		priceProjectVo.setNumber(number);
		priceProjectVo.setProCatagory("造价咨询");
		priceProjectVo.setProCompany(proCompany);
		priceProjectVo.setProContact(proContact);
		priceProjectVo.setProName(proName);
		priceProjectVo.setProContactPhone(proContactPhone);
		priceProjectVo.setProRegion(proRegion);
		priceProjectVo.setReceiveDate(receiveDate);
		priceProjectVo.setRecordUserId(this.getSessionUser().getId());
		priceProjectVo.setRemark(remark);
		priceProjectVo.setStage(1);
		priceProjectVo.setContactUser((UserInfoVo) userInfoService
				.getUserInfo(this.getContactUserId()));
		priceProjectVo.setSubmit(submit);
		priceProjectVo.setSubmitPrice(submitPrice);
		// priceProjectVo.setSubmitUserId(this.getSessionUser().getId());
		priceProjectVo.setWorkCatagory(workCatagory);
		return priceProjectVo;
	}

	public void changeLabel() {
		this.getRequest().setAttribute("isDeny", new Boolean(false));
		if ("2".equals(currentStage) || "3".equals(currentStage)) {
			this.getRequest().setAttribute("permitLabel", "自校成功");
			this.getRequest().setAttribute("denyLabel", "自校失败");
			if("2".equals(currentStage))
			this.getRequest().setAttribute("isDeny", new Boolean(true));
		} else if ("4".equals(currentStage) || "5".equals(currentStage)) {
			this.getRequest().setAttribute("permitLabel", "复核成功");
			this.getRequest().setAttribute("denyLabel", "复核失败");
			if("4".equals(currentStage))
			this.getRequest().setAttribute("isDeny", new Boolean(true));
		} else if ("6".equals(currentStage) || "7".equals(currentStage)) {
			this.getRequest().setAttribute("permitLabel", "复核成功");
			this.getRequest().setAttribute("denyLabel", "复核失败");
			if("6".equals(currentStage))
			this.getRequest().setAttribute("isDeny", new Boolean(true));
			
		}else if ("8".equals(currentStage) || "9".equals(currentStage)) {
			this.getRequest().setAttribute("permitLabel", "签发成功");
			this.getRequest().setAttribute("denyLabel", "签发失败");
			if("8".equals(currentStage))
			this.getRequest().setAttribute("isDeny", new Boolean(true));
		}else if ("10".equals(currentStage)) {
			this.getRequest().setAttribute("permitLabel", "报告成功");
		} else if ("11".equals(currentStage)) {
			this.getRequest().setAttribute("permitLabel", "上报成功");
		}
	}

	public void setPriceProjectService(PriceProjectService priceProjectService) {
		this.priceProjectService = priceProjectService;
	}

	public PriceProjectService getPriceProjectService() {
		return priceProjectService;
	}

	public void setUserInfoService(UserInfoService userInfoService) {
		this.userInfoService = userInfoService;
	}

	public UserInfoService getUserInfoService() {
		return userInfoService;
	}

	public void setId(int id) {
		this.id = id;
	}

	public int getId() {
		return id;
	}

	public void setNumber(String number) {
		this.number = number;
	}

	public String getNumber() {
		return number;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getName() {
		return name;
	}

	public void setDelegateCompany(String delegateCompany) {
		this.delegateCompany = delegateCompany;
	}

	public String getDelegateCompany() {
		return delegateCompany;
	}

	public void setDelegateUser(String delegateUser) {
		this.delegateUser = delegateUser;
	}

	public String getDelegateUser() {
		return delegateUser;
	}

	public void setDelegatePhone(String delegatePhone) {
		this.delegatePhone = delegatePhone;
	}

	public String getDelegatePhone() {
		return delegatePhone;
	}

	public void setProCatagory(String proCatagory) {
		this.proCatagory = proCatagory;
	}

	public String getProCatagory() {
		return proCatagory;
	}

	public void setReceiveDate(Date receiveDate) {
		this.receiveDate = receiveDate;
	}

	public Date getReceiveDate() {
		return receiveDate;
	}


	public String getContractId() {
		return contractId;
	}

	public void setContractId(String contractId) {
		this.contractId = contractId;
	}

	public void setEstimatePrice(String estimatePrice) {
		this.estimatePrice = estimatePrice;
	}

	public String getEstimatePrice() {
		return estimatePrice;
	}

	public void setSubmitPrice(String submitPrice) {
		this.submitPrice = submitPrice;
	}

	public String getSubmitPrice() {
		return submitPrice;
	}

	public void setProRegion(String proRegion) {
		this.proRegion = proRegion;
	}

	public String getProRegion() {
		return proRegion;
	}

	public void setIndustryCatagory(String industryCatagory) {
		this.industryCatagory = industryCatagory;
	}

	public String getIndustryCatagory() {
		return industryCatagory;
	}

	public void setSubmitUserId(int submitUserId) {
		this.submitUserId = submitUserId;
	}

	public int getSubmitUserId() {
		return submitUserId;
	}

	public void setRemark(String remark) {
		this.remark = remark;
	}

	public String getRemark() {
		return remark;
	}

	public void setProName(String proName) {
		this.proName = proName;
	}

	public String getProName() {
		return proName;
	}

	public void setProCompany(String proCompany) {
		this.proCompany = proCompany;
	}

	public String getProCompany() {
		return proCompany;
	}

	public void setProContact(String proContact) {
		this.proContact = proContact;
	}

	public String getProContact() {
		return proContact;
	}

	public void setProContactPhone(String proContactPhone) {
		this.proContactPhone = proContactPhone;
	}

	public String getProContactPhone() {
		return proContactPhone;
	}

	public void setWorkCatagory(String workCatagory) {
		this.workCatagory = workCatagory;
	}

	public String getWorkCatagory() {
		return workCatagory;
	}

	public void setStage(int stage) {
		this.stage = stage;
	}

	public int getStage() {
		return stage;
	}

	public void setAuthorDate(Date authorDate) {
		this.authorDate = authorDate;
	}

	public Date getAuthorDate() {
		return authorDate;
	}

	public void setSubmit(int submit) {
		this.submit = submit;
	}

	public int getSubmit() {
		return submit;
	}

	public void setRecordUserId(int recordUserId) {
		this.recordUserId = recordUserId;
	}

	public int getRecordUserId() {
		return recordUserId;
	}

	public String getMethod() {
		return method;
	}

	public void setMethod(String method) {
		this.method = method;
	}

	public void setContactUser(UserInfoVo contactUser) {
		this.contactUser = contactUser;
	}

	public UserInfoVo getContactUser() {
		return contactUser;
	}

	public void setDeleteId(String deleteId) {
		this.deleteId = deleteId;
	}

	public String getDeleteId() {
		return deleteId;
	}

	public void setModifyId(String modifyId) {
		this.modifyId = modifyId;
	}

	public String getModifyId() {
		return modifyId;
	}

	public void setCheckId(String checkId) {
		this.checkId = checkId;
	}

	public String getCheckId() {
		return checkId;
	}

	public void setRegeditId(String regeditId) {
		this.regeditId = regeditId;
	}

	public String getRegeditId() {
		return regeditId;
	}

	public void setContactUserId(int contactUserId) {
		this.contactUserId = contactUserId;
	}

	public int getContactUserId() {
		return contactUserId;
	}

	public void setCheckPage(String checkPage) {
		this.checkPage = checkPage;
	}

	public String getCheckPage() {
		return checkPage;
	}

	public void setCurrentStage(String currentStage) {
		this.currentStage = currentStage;
	}

	public String getCurrentStage() {
		return currentStage;
	}

	public void setMultiDelete(String multiDelete) {
		this.multiDelete = multiDelete;
	}

	public String getMultiDelete() {
		return multiDelete;
	}

	public void setDelStr(String delStr) {
		this.delStr = delStr;
	}

	public String getDelStr() {
		return delStr;
	}

	public void setMultiType(String multiType) {
		this.multiType = multiType;
	}

	public String getMultiType() {
		return multiType;
	}

	public void setOutputValue(String outputValue) {
		this.outputValue = outputValue;
	}

	public String getOutputValue() {
		return outputValue;
	}

	public void setProFileService(ProFileService proFileService) {
		this.proFileService = proFileService;
	}

	public ProFileService getProFileService() {
		return proFileService;
	}

	public void setServletRequest(HttpServletRequest request) {
		// TODO Auto-generated method stub
		this.request = request;
	}

	public void setServletResponse(HttpServletResponse response) {
		// TODO Auto-generated method stub
		this.response = response;
	}

	public void setLeaderId(int leaderId) {
		this.leaderId = leaderId;
	}

	public int getLeaderId() {
		return leaderId;
	}

	public void setRecordMemberService(RecordMemberService recordMemberService) {
		this.recordMemberService = recordMemberService;
	}

	public RecordMemberService getRecordMemberService() {
		return recordMemberService;
	}

	public void setUserStr(String userStr) {
		this.userStr = userStr;
	}

	public String getUserStr() {
		return userStr;
	}

	public void setProSuggestionService(ProSuggestionService proSuggestionService) {
		this.proSuggestionService = proSuggestionService;
	}

	public ProSuggestionService getProSuggestionService() {
		return proSuggestionService;
	}

	public void setMemList(List<String> memList) {
		this.memList = memList;
	}

	public List<String> getMemList() {
		return memList;
	}

	public void setTrailState(int trailState) {
		this.trailState = trailState;
	}

	public int getTrailState() {
		return trailState;
	}

	public void setIsMajor(int isMajor) {
		this.isMajor = isMajor;
	}

	public int getIsMajor() {
		return isMajor;
	}
}
